package com.shujia.flink.core

import org.apache.flink.streaming.api.scala._

object Demo1WordCountStream {
  def main(args: Array[String]): Unit = {
    //1、创建flink的运行环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置并行度
    //env.setParallelism(2)

    /**
     * 数据从下游发送到下游的超时时间
     */
    env.setBufferTimeout(200)

    //2、读取数据
    //nc -lk 8888
    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)
    //3、统计单词的数量
    //一行转换成多行
    val wordsDS: DataStream[String] = linesDS.flatMap(_.split(","))
    //转换成kv格式
    val kvDS: DataStream[(String, Int)] = wordsDS.map(word => (word, 1))

    //安装单词进行分组
    val keyBHyDS: KeyedStream[(String, Int), String] = kvDS.keyBy(kv => kv._1)

    //统计数量
    val countDS: DataStream[(String, Int)] = keyBHyDS.sum(1)

    //打印结果
    countDS.print()

    //启动flink
    env.execute()
  }
}
